import time
from functools import wraps
from env import dataSource

def mysqli(func , disableFk=True):
	@wraps(func)
	def wrapper(*args , **kwargs):
		st = time.time()
		varnames = func.__code__.co_varnames
		con = None
		print('varnames',varnames)
		if 'con' in varnames :
			con = dataSource.createConnection()
			if disableFk :
				con.disableFk()
			kwargs['con'] = con
		try:
			result = func(*args , **kwargs)
		finally:
			if con is not None :
				con.commit()
				con.close()
		et = time.time()
		print(func.__name__, str(et-st)+" sec")
		return result
	return wrapper


def mysql(tx=False):
	def decorator(func):
		def wrapper(*args, **kw):
			st = time.time()
			varnames = func.__code__.co_varnames
			con = None
			print('varnames',varnames)
			if 'con' in varnames :
				con = dataSource.createConnection()
				if tx :
					con.setAutoCommit(False)
				else :
					con.setAutoCommit(True)
				kw['con'] = con
			try:
				result = func(*args , **kw)
			finally:
				if con is not None :
					if tx :
						con.commit()
					con.close()
			et = time.time()
			print(func.__name__, str(et-st)+" sec")
			return result
		return wrapper
	return decorator